package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.util.JSONResult;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.Callable;

/**
 * 注册登录相关
 */
@Controller
public class LogininfoController {
	@Autowired
	private ILogininfoService logininfoService;
	@RequestMapping("register")@ResponseBody
	public JSONResult register(String username,String password) {
		JSONResult result = new JSONResult();
		try {
			logininfoService.register(username, password,Logininfo.USER_CLIENT);
			result.setMsg("注册成功");
		}catch(Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("用户注册失败,原因:"+e.getMessage());
		}
		return result;
		
	}
	@RequestMapping("checkUsername")@ResponseBody
	public boolean checkUserName(String username){
		return !logininfoService.checkUsername(username,Logininfo.USER_CLIENT);
	}
	/** 
	* @Description:
	* @Param:  
	* @returns:  
	* @Author: jining
	* @Date: 2019/4/18 13:53
	*/
	@RequestMapping("login")@ResponseBody
	public JSONResult login(String username, String password, HttpServletRequest request) {
		JSONResult result = new JSONResult();

		try {
			String ip =request.getRemoteAddr();
			Logininfo login = logininfoService.login(username, password, ip,Logininfo.USER_CLIENT);

			if(login==null){
				result.setSuccess(false);
				result.setMsg("用户账号密码错误!");
			}else{

				result.setMsg("登录成功");
			}
		}catch(Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("用户登录失败,原因:"+e.getMessage());
		}
        //Logininfo current = UserContext.getCurrent();
        //System.out.println(current.getUsername());
		return result;

	}
	



}
